package it.unibz.ti2.internetPizzeria.servlets;

import it.unibz.ti2.internetPizzeria.dao.ProductDAO;
import it.unibz.ti2.internetPizzeria.dao.UserDAO;

import it.unibz.ti2.internetPizzeria.entities.Customer;
import it.unibz.ti2.internetPizzeria.entities.Drink;
import it.unibz.ti2.internetPizzeria.entities.Pizza;
import it.unibz.ti2.internetPizzeria.utils.ServletUtil;
import it.unibz.ti2.internetPizzeria.webAttributes.UserInfo;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Listing chosen products and chosen address to deliver
 */
public class NewOrderConfirmation extends HttpServlet {

	private static final long serialVersionUID = -647408377383398736L;

	protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
			throws ServletException, IOException {
		this.doPost(arg0, arg1);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ProductDAO productDAO = null;
		try {
			productDAO = new ProductDAO();
			productDAO.openSession();
			UserDAO userDAO = new UserDAO();
			userDAO.openSession(productDAO);

			String[] pizzasIds = request.getParameterValues("pizzasIds");
			String[] drinksIds = request.getParameterValues("drinksIds");

			Map<Pizza, Integer> pizzasMap = new TreeMap<Pizza, Integer>();
			Map<Drink, Integer> drinksMap = new TreeMap<Drink, Integer>();

			Float price = new Float(0);

			for (int i = 0; i < pizzasIds.length; i++) {
				Integer quantity = new Integer(request.getParameter("select"
						+ pizzasIds[i]));
				if (quantity != 0) {
					Pizza pizza = productDAO.getPizza(Integer
							.parseInt(pizzasIds[i]));
					pizzasMap.put(pizza, quantity);
					price += quantity * pizza.getPrice();
				}
			}
			for (int i = 0; i < drinksIds.length; i++) {
				Integer quantity = new Integer(request.getParameter("select"
						+ drinksIds[i]));
				if (quantity != 0) {
					Drink drink = productDAO.getDrink(Integer
							.parseInt(drinksIds[i]));
					drinksMap.put(drink, quantity);
					price += quantity * drink.getPrice();
				}
			}

			UserInfo userInfo = (UserInfo) request.getSession().getAttribute(
					"userInfo");

			if (userInfo.getUserType().equals("Customer")) {
				Customer customer = (Customer) userDAO
						.getUser(userInfo.getId());
				if (customer != null) {
					request.setAttribute("name", customer.getName());
					request.setAttribute("surname", customer.getSurname());

					if (request.getParameter("group1").equals("defaultAddress")) {
						request.setAttribute("city", customer.getCity());
						request.setAttribute("streetAndNumber", customer
								.getStreetAndNumber());
						request.setAttribute("country", customer.getCountry());
						request.setAttribute("zipCode", customer.getZipCode());

					} else if (request.getParameter("group1").equals(
							"anotherAddress")) {
						request.setAttribute("city", request
								.getParameter("userCity"));
						request.setAttribute("streetAndNumber", request
								.getParameter("userStreetAndNumber"));
						request.setAttribute("country", request
								.getParameter("userCountry"));
						request.setAttribute("zipCode", request
								.getParameter("userZipCode"));
					}

					request.setAttribute("pointLat", new Float(Float
							.parseFloat(request.getParameter("pointLat"))));
					request.setAttribute("pointLng", new Float(Float
							.parseFloat(request.getParameter("pointLng"))));

					request.setAttribute("feeForDelivery",
							new Float(Float.parseFloat(request
									.getParameter("feeForDelivery"))));
					request.setAttribute("isCustomer", new Boolean(true));
				} else {
					request.setAttribute("isCustomer", new Boolean(false));
				}
			} else {
				request.setAttribute("isCustomer", new Boolean(false));
			}
			request.setAttribute("price", price);
			request.setAttribute("pizzasMap", pizzasMap);
			request.setAttribute("drinksMap", drinksMap);
			ServletUtil
					.redirect(request, response, "/newOrderConfirmation.jsp");
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (productDAO != null) {
				productDAO.closeSession();
			}
		}

	}
}
